#size
res<-300
width<-500*res/72
height<-4/5*width

color<-c('gray80','gray20', 'gray50')
space<-c(0,2)#spaces between pairs of bars
border<-T
lwd_border<-0.5

#grid
lwd_grid<-0.5
color_grid<-"black"

#Data

mydata <- read.csv("data/Covidrisk_wave1.csv", header=T)

percent.selfrestored <- mean(mydata$selfrestoredhrs)*100
percent.selfreduced <- mean(mydata$selfreducedhrs)*100
percent.selfrehired <- mean(mydata$selfrestoredlayoff)*100
percent.selftemp <- mean(mydata$selftemplayoff)*100
percent.selfperm <- mean(mydata$selflayoff)*100

percent.familyrestored <- mean(mydata$familyrestoredhrs)*100
percent.familyreduced <- mean(mydata$familyreducedhrs)*100
percent.familyrehired <- mean(mydata$familyrestoredlayoff)*100
percent.familytemp <- mean(mydata$familytemplayoff)*100
percent.familyperm <- mean(mydata$familylayoff)*100

percent.none <- mean(mydata$noeconshock)*100

self<-c(percent.selfrestored,percent.selfreduced,percent.selfrehired,percent.selftemp,percent.selfperm, 0)
family<-c(percent.familyrestored,percent.familyreduced,percent.familyrehired,percent.familytemp,percent.familyperm, 0)
none <-c(0,0,0,0,0,percent.none)
 
#Export to tiff
tiff(filename = "figures_tables/Figure3.tif",
     width = width, 
     height =height,
     res=res,
     units = "px", 
     pointsize=9,
     bg = "white", 
     type = 'cairo')

par(mar=c(4,6,3,4)+0.1,lwd=lwd_border)

barplot(rbind(self, family, none), width=0.3, xlim=c(0,6), ylim=c(0,60),beside=T,
        col=color,,space=c(0.5,0,0,0.75,0,0,0.75,0,0,0.75,0,0,0.75,0,0,-1,0,0))
        
abline(h=seq(0,60,5),col=color_grid,lwd=lwd_grid)
abline(h=0)
abline(h=60)

mtext(c('Reduced hrs/restored', 'Reduced hrs', 'Laid off/rehired', 'Laid off (temp.)', 'Laid off (perm.)', 'None'),side=1,line=2,at=c(0.35,1.55,2.55,3.7,4.85,5.9)+0.1)

barplot(rbind(self, family, none), width=0.3, xlim=c(0,6), ylim=c(0,60),beside=T,
        col=color,lwd=lwd_border,space=c(0.5,0,0,0.75,0,0,0.75,0,0,0.75,0,0,0.75,0,0,-1,0,0),
        ylab='Percentage of respondents',add=T)


legend(0.25,50,
       legend=c('Self','Family', 'None'),
       fill=c('gray80','gray20', 'gray50'),
       bty='n'
)

dev.off()